package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.excel.utils.ExcelUtil;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.ScreenBo;
import com.wande.dataplatform.domain.vo.ScreenVO;
import com.wande.dataplatform.service.IScreenService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 大屏设计控制器
 *
 * @author wande
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/screen")
public class ScreenController extends BaseController {

    private final IScreenService screenService;

    /**
     * 查询大屏设计列表
     */
    @SaCheckPermission("dataplatform:screen:list")
    @GetMapping("/list")
    public TableDataInfo<ScreenVO> list(ScreenBo bo, PageQuery pageQuery) {
        return screenService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出大屏设计列表
     */
    @SaCheckPermission("dataplatform:screen:export")
    @Log(title = "大屏设计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ScreenBo bo, HttpServletResponse response) {
        List<ScreenVO> list = screenService.queryList(bo);
        ExcelUtil.exportExcel(list, "大屏设计", ScreenVO.class, response);
    }

    /**
     * 获取大屏设计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataplatform:screen:query")
    @GetMapping("/{id}")
    public R<ScreenVO> getInfo(@PathVariable Long id) {
        return R.ok(screenService.queryById(id));
    }

    /**
     * 新增大屏设计
     */
    @SaCheckPermission("dataplatform:screen:add")
    @Log(title = "大屏设计", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Valid @RequestBody ScreenBo bo) {
        return toAjax(screenService.insertByBo(bo));
    }

    /**
     * 修改大屏设计
     */
    @SaCheckPermission("dataplatform:screen:edit")
    @Log(title = "大屏设计", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody ScreenBo bo) {
        return toAjax(screenService.updateByBo(bo));
    }

    /**
     * 删除大屏设计
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataplatform:screen:remove")
    @Log(title = "大屏设计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        return toAjax(screenService.deleteByIds(ids));
    }

    /**
     * 发布大屏
     *
     * @param id 大屏ID
     */
    @SaCheckPermission("dataplatform:screen:publish")
    @Log(title = "大屏设计", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/publish")
    public R<Void> publish(@PathVariable Long id) {
        return toAjax(screenService.publishScreen(id));
    }

    /**
     * 下线大屏
     *
     * @param id 大屏ID
     */
    @SaCheckPermission("dataplatform:screen:offline")
    @Log(title = "大屏设计", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/offline")
    public R<Void> offline(@PathVariable Long id) {
        return toAjax(screenService.offlineScreen(id));
    }

    /**
     * 获取预览URL
     *
     * @param id 大屏ID
     */
    @SaCheckPermission("dataplatform:screen:query")
    @GetMapping("/{id}/preview-url")
    public R<String> getPreviewUrl(@PathVariable Long id) {
        return R.ok(screenService.getPreviewUrl(id));
    }

    /**
     * 查询大屏组件数据
     *
     * @param id 大屏ID
     * @param componentId 组件ID
     */
    @GetMapping("/{id}/data/{componentId}")
    public R<Object> queryScreenData(@PathVariable Long id, @PathVariable String componentId) {
        return R.ok(screenService.queryScreenData(id, componentId));
    }
}
